home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 226 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.1 KB  |  107 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: (no subject)
  5. Date: 31 Jan 1996 16:34:17 GMT
  6. Organization: SEL
  7. Sender: news@lts.sel.alcatel.de
  8. Approved: clamage@eng.sun.com (comp.std.c++)
  9. Message-ID: <KANZE.96Jan31171932@slsvewt.lts.sel.alcatel.de>
  10. References: <4elfbq$21r@bmtlh10.bnr.ca>
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. Content-Type: text
  13. In-Reply-To: "john's message of 30 Jan 1996 09:51:25 PST
  14. Apparently-To: std-c++@ncar.ucar.edu
  15. Content-Length: 3412
  16. X-Lines: 83
  17. Originator: clamage@taumet
  18.  
  19. In article <4elfbq$21r@bmtlh10.bnr.ca> "john (j.d.) hickin"
  20. <hickin@bnr.ca> writes:
  21.  
  22. |> Some current compilers allocate memory for heap objects from inside
  23. |> the constructor code.  This isn't great when exceptions can be thrown
  24. |> and indeed generated code from compilers that support exception handling
  25. |> seems to abandon the idea.
  26.  
  27. |> Does the standard say anything about how memory should be allocated?
  28.  
  29. Yes and no.  The defined semantics of the new operator are that the
  30. memory is allocated, then the constructor is called.  However, the
  31. compiler can do anything it wants, as long as the `observable
  32. behavior' is the same.  Since the physical function call is not part
  33. of the observable behavior, the implementation is perfectly free to
  34. generate the call to new in the constructor itself, a la cfront, if it
  35. so desires.  (And I don't really see where this would cause problems
  36. with exception handling.)
  37.  
  38. |> My question is motivated by the following simple program which exhibits
  39. |> different behavior when exception handling is enabled compared to when
  40. |> it isn't.  Would such behavior be accptable inder the provisions of the
  41. |> standard?
  42.  
  43. As far as the standard is concerned, exception handling is always
  44. enabled.  From a quality of implementation point of view, I wouldn't
  45. think very highly of a compiler which changed the semantics of new
  46. when exception handling was disabled.
  47.  
  48. |> // -----------------------------------------------------------------
  49.  
  50. |> #include <stddef.h>
  51.  
  52. |> struct Y
  53. |> {
  54. |>   Y() {}
  55. |>   void* allocate() { return 0; }
  56. |> };
  57.  
  58. |> struct X
  59. |> {
  60. |>    X() {}
  61. |>    void* operator new( size_t ) { return (void*)42; }
  62. |>    void* operator new( size_t , Y& alloc ) { return alloc.allocate(); }
  63. |>    void  operator delete( void* ) {}
  64. |>    //void operator delete( size_t , Y& ); // not yet supported by my compiler
  65. |> };
  66.  
  67.  
  68. |> #include <iostream.h>
  69.  
  70. |> main()
  71. |> {
  72. |>   Y y;
  73. |>   X* ptr = new(y) X;
  74. |>   cout << "ptr == " << (int)ptr << "\n";
  75. |> }
  76.  
  77. |> The output is 0 when exception handling is enabled and 42 when it isn't.
  78.  
  79. The output is 0 when a C++ compiler is used.  It may be anything when
  80. a compiler for another language is used, but the above is a conforming
  81. C++ program, whose observable behavior is to output 0.  Exception
  82. handling has nothing to do with the questions.  (Of course, if you
  83. disable exception handling, you have told your compiler not to compile
  84. C++, but a very similar, slightly smaller language.  Since this
  85. language is defined by the compiler vendor, he could argue that the
  86. above output corresponds to his definition of the language.  See my
  87. comments above on quality of implementation.)
  88.  
  89. (In the above discussion, I'm ignoring the fact that conversion
  90. between int and pointer is implementation defined.  Strictly speaking,
  91. the implementor may define that converting a null pointer to an int
  92. yeilds 42 when exception handling is disabled, and 0 when it isn't.
  93. Somehow, I doubt that this is actually what is happening; modifying
  94. the operator new so that the write a static variable, or output a
  95. message, according to which one was called, should clear this up.)
  96. --
  97. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  98. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  99. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  100.                 -- A la recherche d'une activitΘ dans une region francophone
  101.  
  102.  
  103. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  104.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  105.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  106.  
  107.